package com.yilin.tms.user.controller;

import com.yilin.commons.util.StringUtil;
import com.yilin.tms.core.application.PageData;
import com.yilin.tms.core.application.ReturnData;
import com.yilin.tms.core.commons.annotation.SkipLoginVerify;
import com.yilin.tms.core.commons.base.BaseController;
import com.yilin.tms.core.commons.utils.ParamUtil;
import com.yilin.tms.core.entity.user.account.AdvanceMoney;
import com.yilin.tms.core.entity.truck.TruckVehicle;
import com.yilin.tms.core.remote.IVehicleRemote;
import com.yilin.tms.user.service.impl.AdvanceMoneyService;
import jdk.nashorn.internal.runtime.regexp.joni.ast.StringNode;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user/advance/")
public class AdvanceMoneyController extends BaseController {
    @Resource
    private AdvanceMoneyService advanceMoneyService;
    @Resource
    private IVehicleRemote vehicleRemote;

    @PostMapping("saveOrUpdateAdvanceMoneySubtract")
    @SkipLoginVerify
    public ReturnData<Object> saveOrUpdateAdvanceMoneySubtract(AdvanceMoney advanceMoney) {
        ParamUtil.isNull(advanceMoney, "请传入参数");
        TruckVehicle truckVehicle = null;
        ParamUtil.isNull(advanceMoney.getExpenseType(), "报销类型不能为空");
        ParamUtil.isNull(advanceMoney.getMonery(), "请输入报销金额");
        ParamUtil.isNull(advanceMoney.getOtherCostImgUrl(), "请传入报销证明");
        if (advanceMoney.getExpenseType() == AdvanceMoney.ExpenseType.oill || advanceMoney.getExpenseType() == AdvanceMoney.ExpenseType.vehicle) {
            ParamUtil.isNull(advanceMoney.getTruckId(), "请传入车辆");
            truckVehicle = ParamUtil.isSuccess(vehicleRemote.getVehicleById(advanceMoney.getTruckId()));
            ParamUtil.isNull(truckVehicle, "车辆获取失败");
            advanceMoney.setTruckNo(truckVehicle.getTrackNo());
        }
        advanceMoneyService.saveOrUpdateAdvanceMoneySubtract(getLoginUser(), advanceMoney, truckVehicle);
        return jsonView(true, "保存成功");
    }

    @PostMapping("saveOrUpdateAdvanceMoneyAdd")
    @SkipLoginVerify
    public ReturnData<Object> saveOrUpdateAdvanceMoneyAdd(AdvanceMoney advanceMoney, String accountId) {
        ParamUtil.isNull(advanceMoney, "请传入参数");
        ParamUtil.isNull(advanceMoney.getExpenseType(), "报销类型不能为空");
        ParamUtil.isNull(advanceMoney.getMonery(), "请输入报销金额");
        ParamUtil.isNull(advanceMoney.getOtherCostImgUrl(), "请传入报销证明");
        TruckVehicle truckVehicle = null;
        if (advanceMoney.getExpenseType() == AdvanceMoney.ExpenseType.oill || advanceMoney.getExpenseType() == AdvanceMoney.ExpenseType.vehicle|| advanceMoney.getExpenseType() == AdvanceMoney.ExpenseType.integral) {
            ParamUtil.isNull(advanceMoney.getTruckId(), "请传入车辆");
            truckVehicle = ParamUtil.isSuccess(vehicleRemote.getVehicleById(advanceMoney.getTruckId()));
            ParamUtil.isNull(truckVehicle, "车辆获取失败");
        }
        advanceMoneyService.saveOrUpdateAdvanceMoneyAdd(getLoginUser(),accountId, advanceMoney, truckVehicle);
        return jsonView(true, "保存成功");
    }

    @PostMapping("auditingAdvanceMoney")
    @SkipLoginVerify
    public ReturnData<Object> auditingAdvanceMoney(AdvanceMoney advanceMoney) {
        ParamUtil.isNull(advanceMoney, "请传入参数");
        ParamUtil.isNull(advanceMoney.getId(), "请传入id");
        ParamUtil.isNull(advanceMoney.getAdvanceState(), "请传入状态");
        if (advanceMoney.getAdvanceState() != AdvanceMoney.AdvanceState.create && advanceMoney.getAdvanceState() != AdvanceMoney.AdvanceState.success) {
            return jsonView(false, "状态错误");
        }
        if (advanceMoney.getAdvanceState() == AdvanceMoney.AdvanceState.create && StringUtil.isEmpty(advanceMoney.getResMsg())) {
            return jsonView(false, "请说明原因");
        }
        advanceMoneyService.auditingAdvanceMoney(getLoginUser(), advanceMoney);
        return jsonView(true, "保存成功");
    }


    @PostMapping("getAdvanceMoneyPage")
    @SkipLoginVerify
    public ReturnData<Object> getAdvanceMoneyPage(AdvanceMoney query, Integer page, Integer limit) {
        PageData<AdvanceMoney> data = advanceMoneyService.getAdvanceMoneyPage(getLoginUser(), query, page, limit);
        return jsonView(SUCCESS_CODE, "获取成功", data);
    }

    @PostMapping("getAdvanceMoneyById")
    @SkipLoginVerify
    public ReturnData<Object> getAdvanceMoneyById(String advanceId) {
        AdvanceMoney data = advanceMoneyService.getAdvanceMoneyById(advanceId);
        return jsonView(SUCCESS_CODE, "获取成功", data);
    }

}
